package mysql

import (
	"develop/usersrpc/usersrpc"
	"gorm.io/gorm"
)

type Video_Info struct {
	gorm.Model
	Upload_User_Id int    `gorm:"type:int(11);comment:'上传用户ID'"`
	Video_Title    string `gorm:"type:varchar(30);not null;comment:'视频标题'"`
	Video_Category string `gorm:"type:varchar(30);not null;comment:'视频分类'"`
	Video_Link     string `gorm:"type:varchar(500);not null;comment:'视频URL'"`
	Cover_Link     string `gorm:"type:varchar(500);not null;comment:'视频封面URL'"`
	Video_Intro    string `gorm:"type:varchar(500);comment:'视频简介'"`
	Play_Num       int    `gorm:"type:int(11);comment:'播放量'"`
	Like_Num       int    `gorm:"type:int(11);comment:'点赞数'"`
	Collect_Num    int    `gorm:"type:int(11);comment:'收藏数'"`
	Comment_Num    int    `gorm:"type:int(11);comment:评论数'"`
	Release_Time   string `gorm:"type:varchar(50);comment:发布时间'"`
	Video_Status   int    `gorm:"type:int(11);comment:'视频状态'"`
	Topic_Id       int    `gorm:"type:int(11);comment:'关联话题ID'"`
}

func (i *Video_Info) VideoCreate(db *gorm.DB) error {
	return db.Debug().Create(i).Error
}

func (i *Video_Info) FindVideoId(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Find(i).Error
}

func (i *Video_Info) UpdateInfo(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Updates(i).Error
}

func (i *Video_Info) DelInfo(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Delete(i).Error
}

func VideoList(db *gorm.DB, in *usersrpc.VideoListRequest) []*usersrpc.VideoList {
	var videoList []*usersrpc.VideoList
	db.Debug().Model(&Video_Info{}).Select(" video_infos.play_num,\n  video_infos.cover_link,\n  video_infos.video_title,\n  video_infos.upload_user_id").
		Joins(" LEFT JOIN user_infos ON video_infos.upload_user_id = user_infos.id").
		Order("release_time desc").
		Scopes(Paginate(int(in.Page), int(in.PageSize))).Find(&videoList)
	return videoList
}
